package com.exonline.front.controller.subject;

import cn.hutool.core.util.IdUtil;
import com.exonline.commons.base.IBaseServlet;
import com.exonline.commons.entity.Subject;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc 科目的C层
 * @author  hbrj
 *
 */
@WebServlet("/font/subject")
public class subjectServlet extends HttpServlet implements IBaseServlet {
    ObjectMapper mapper = new ObjectMapper();
    subjectService service = new subjectService();


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        try {
            if ("query".equals(method)) {
                this.query(req, resp);
            } else if ("queryone".equals(method)) {
                this.queryOne(req, resp);
            } else if ("add".equals(method)) {
                this.add(req, resp);
            } else if ("edit".equals(method)) {
                this.edit(req, resp);
            } else if ("delete".equals(method)) {
                this.delete(req, resp);
            }
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }

    /**
     * @desc  查询
     * @param req
     * @param resp
     * @throws SQLException
     * @throws IOException
     */
    @Override
    public void query(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        resp.setHeader("Content-Type", "application/json;charset=utf-8");
        int pageNo = Integer.parseInt(req.getParameter("pageno"));
        List<Subject> subjects = service.queryAll(1,pageNo);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", subjects);
        mapper.writeValue(resp.getWriter(),map);
    }

    /**
     * @desc  删除
     * @param req
     * @param resp
     * @throws SQLException
     * @throws IOException
     */
    @Override
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = req.getParameter("id");
        service.deleteById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除成功");
        map.put("data", id);
        mapper.writeValue(resp.getWriter(), map);
    }

    /**
     * @desc  查询单个
     * @param req
     * @param resp
     * @throws SQLException
     * @throws IOException
     */
    @Override
    public void queryOne(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = req.getParameter("id");
        Subject subject = service.queryOne(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", subject);
        mapper.writeValue(resp.getWriter(), map);
    }

    /**
     * @desc  编辑
     * @param req
     * @param resp
     * @throws SQLException
     * @throws IOException
     */
    @Override
    public void edit(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("suname");
        service.update(id,name);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", id);
        mapper.writeValue(resp.getWriter(), map);
    }

    /**
     * @desc  添加
     * @param req
     * @param resp
     * @throws SQLException
     * @throws IOException
     */
    @Override
    public void add(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = IdUtil.simpleUUID();
        String name = req.getParameter("suname");
        service.add(id,name);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "添加成功");
        map.put("data", id);
        mapper.writeValue(resp.getWriter(), map);
    }
}
